/**
 * Created by jacky on 2018/4/8.
 */


var api = {
    domain:'http://'+document.domain+'/',
    winName:'wap',
    openFrame: function(json) {
        window.location.href = this.getPath(json);
    },
    openWin: function(json) {
         if (api.winName=='wap') {
           url = this.domain+'html/'+json.pageParam.frameName+'.html';
           window.location.href=url;
           return;
         }
        window.location.href = this.getPath(json);
    },
    closeWin: function() {
        history.back(-1);
    },
    showProgress: function() {

    },
    hideProgress: function() {

    },
    getPath: function(json) {
        var url = json.url
        try {
            i = 0;
            for (var key in json.pageParam) {
                if (key) {
                    url += i == 0 ? "?" + key + "=" + json.pageParam[key] : "&" + key + "=" + json.pageParam[key];
                    i++;
                }
            }
            url = url.replace(/widget:\/\//,this.domain);
        } catch (err) {

        }
        return url;
    },
    getJson: function(json) {
        if (typeof json != "object") {
            return JSON.parse(json);
        }
        return json;
    },
    setAnimate: function() {
        $(".j-custom").each(function(i, list) {
            $(list).animate({
                bottom: 70 + "px"
            }, 500);
        });
    },
    removeAnimate: function(list) {
        setTimeout(function() {
            $(list.element).animate({
                opacity: 0
            }, 500, function() {
                $(list.element).remove();
                base.vars.arr.pop();
                this.setAnimate();
            }.bind(this))
        }.bind(this), 2000 + base.vars.arr.length * 500)
    },
    toast: function(msg) {
        console.log(msg)

        if (msg.msg) {
            msg = msg.msg;
        }
        console.log(msg)
        var html = "<div class='j-custom' style='z-index: 2000;max-width:60%;position: fixed;bottom: 0;background: #333;color: #fff;height: 40px;line-height: 40px;border-radius: 5px;padding: 0 15px;font-size: 14px;left: 50%;-ms-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%);transform: translate(-50%, -50%);white-space: nowrap;overflow: hidden;text-overflow: ellipsis;'><span>" + msg + "</span></div>";
        var count = $(".j-custom").length;
        var element = $(html).animate({
            bottom: 70 + "px",
            zIndex: 99999
        });
        $(".j-custom").remove();
        $("body").append(element);
        base.vars.arr = [{
            element: element
        }];
        this.removeAnimate({
            element: element
        });
    },
    ajax: function(json, callback, error, method) {

        var data = json.data.values;
        var url = json.url;
        var that = this;
        $.ajax({
            type: method ? method : "POST",
            data: data,
            url: url,
            dataType: 'html',
            success: function(xhr) {
               //  console.log(xhr)
                var json = that.getJson(xhr);
                if (json.status) {
                    if (callback) {
                        callback(json, data, url);
                    }
                } else {
                    if (error) {
                        error(json);
                    } else {
                        if (callback) {
                            callback(json, data, url);
                        }
                    }
                }
            },
            error: function(xhr) {
               //  console.log(xhr)
                if (xhr.statusText == "error") {
                    var str = window.location.href.split('/');
                    str = str[str.length - 1].replace('.html', '.json');
                    var url = getRealPath() + "/bixinxi/json/" + str;
                    that.ajax(data, url, callback, error, method)
                }
            },
            complete: function(data) {
                "use strict";

                if (data.status == 404) {
                    that.toast('网络资源请求失败');
                }
            }
        })
    }
}



function initapiready() {
    try {
        apiready();
    } catch (err) {
        if (!window.apiready) {
            setTimeout(function() {
                initapiready();
            }, 10);
        }
    }
}

initapiready();
